
package cn.IT.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.Knowledge;
import cn.IT.domain.Topic;
import cn.IT.domain.Type;
import cn.IT.domain.User;
import cn.IT.service.IKnowledgeService;
import cn.IT.service.ITopicService;
import cn.IT.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller()
@Scope("prototype")

public class KnowledgeAction {
    
	private User user;
	private Type type;
	private Knowledge knowledge;
	private String searchContent;
	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private IKnowledgeService knowledgeService;

    JsonConfig jsonConfig = new JsonConfig();  
    
	
	//根据标题搜索帖子
	public String getAllKnowledgeBySearchContent(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Knowledge> knowledges = knowledgeService.getAllKnowledgeBySearchContent(searchContent);
		if( knowledges != null){
			resultData = "{\"result\": \"success\"" + "," + "\"knowledges\":" + JSONArray.fromObject(knowledges,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//根据类型获取知识
	public String getKnowledgessByType(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Knowledge> knowledges = knowledgeService.getAllKnowledgeByType(type);
		if( knowledges != null){
			resultData = "{\"result\": \"success\"" + "," + "\"knowledges\":" + JSONArray.fromObject(knowledges,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取用户贡献的知识
	public String getAllKnowledgeByAuthor(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		List<Knowledge> knowledges = knowledgeService.getAllKnowledgeByAuthor(user);
		if( knowledges != null){
			resultData = "{\"result\": \"success\"" + "," + "\"knowledges\":" + JSONArray.fromObject(knowledges,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//根据id获取知识
	public String getKnowledgeById(){
		Knowledge comknowledge = knowledgeService.getKnowledgeById(knowledge.getKnowledgeId());
		Integer count = knowledgeService.getKnowledgeCount();
		if( comknowledge != null){
			resultData = "{\"result\": \"success\"" + "," + "\"knowledge\":" + JSONObject.fromObject(comknowledge) + "," + "\"count\":" + count + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}

	public String getSearchContent() {
		return searchContent;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Knowledge getKnowledge() {
		return knowledge;
	}

	public void setKnowledge(Knowledge knowledge) {
		this.knowledge = knowledge;
	}


}
